package com.chen.palmar.base;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.chen.palmar.R;
import com.flyco.dialog.widget.NormalDialog;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;

import butterknife.ButterKnife;
import dmax.dialog.SpotsDialog;
import rx.subscriptions.CompositeSubscription;


public abstract class BaseActivity extends AutoLayoutActivity {
    /**
     * activity管理集合，退出时把activity销毁掉
     */
    public static ArrayList<Activity> activityList = new ArrayList<>();
    public CompositeSubscription subscription;
    public SpotsDialog spotsDialog;

    /**
     * 初始化操作
     */
    public abstract void init();

    /**
     * 布局ID
     */
    public abstract int getLayoutId();

    public void preBaseWindow() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 去除actionbar，必须放在setContentView前面
         */
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        preBaseWindow();
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        activityList.add(this);
        subscription = new CompositeSubscription();
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    public void setStatusBarColor(Toolbar toolbar) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);

            //获取status_bar_height资源的ID
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
                toolbar.setPadding(0, statusBarHeight, 0, 0);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

            //获取status_bar_height资源的ID
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
                toolbar.setPadding(0, statusBarHeight, 0, 0);
            }
        }
    }

    /**
     * 防止用户更改字体导致布局变形
     */
    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return resources;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 程序退出
     */
    public static void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        activityList.clear();
        //        杀死进程
        android.os.Process.killProcess(android.os.Process.myPid());
        //        通知垃圾回收器回收垃圾
        System.gc();
        //        退出
        System.exit(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (null != subscription) {
            subscription.clear();
            subscription.unsubscribe();
        }
        if (spotsDialog != null && spotsDialog.isShowing()) {
            hideLoading();
        }
    }

    public void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    public NormalDialog showAlterDialog(String title, String message) {
        NormalDialog dialog = new NormalDialog(this);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.titleLineColor(ContextCompat.getColor(this, R.color.colorPrimary));
        dialog.title(title);
        dialog.widthScale(0.7f);
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(this, R.color.line));
        dialog.show();
        return dialog;
    }

    public NormalDialog showAlterDialogOne(String title, String message) {
        NormalDialog dialog = new NormalDialog(this);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.btnNum(1);
        dialog.widthScale(0.7f);
        dialog.titleLineColor(ContextCompat.getColor(this, R.color.colorPrimary));
        dialog.title(title);
        dialog.btnText("确定");
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(this, R.color.line));
        dialog.show();
        return dialog;
    }

    public SpotsDialog showLoading(String text) {
        if (null == spotsDialog) {
            spotsDialog = new SpotsDialog(this, text);
        }
        spotsDialog.show();
        return spotsDialog;
    }

    public SpotsDialog showLoading() {
        if (null == spotsDialog) {
            spotsDialog = new SpotsDialog(this, "加载中");
        }
        spotsDialog.show();
        return spotsDialog;
    }

    public void hideLoading() {
        if (null != spotsDialog) {
            spotsDialog.dismiss();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && spotsDialog != null && spotsDialog.isShowing()) {
            if (null != subscription) {
                subscription.clear();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

}
